package mwa.cs.cr.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mwa.cs.dao.RouteDAO;
import mwa.cs.dao.SMListOfValueDAO;
import mwa.cs.dao.ZoneDAO;
import mwa.cs.database.Database;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class CS24001Srvl
 */
public class CS24001Srvl extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private int vnSiteId;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS24001Srvl() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String vaProcesType = request.getParameter("process_type");
        vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
        if (vaProcesType.equals("getZone")) {
                getZone(request,response);
        } else if (vaProcesType.equals("getRoute")){
                getRoute(request,response);
        } else if (vaProcesType.equals("getStatus")) {
                getStatus(request,response);
        }
    }
    
    @SuppressWarnings("unchecked")
    public void getZone(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8"); 
                response.setContentType("text/JSON");
                String[] vaMessage = null, vaValue = null;
                Database db = new Database();
        
        ZoneDAO voZoneDAO = new ZoneDAO(db);
                List<Map<String, Object>> voResult = voZoneDAO.findZoneBySite(vnSiteId);
                vaValue = new String[voResult.size()];
                vaMessage = new String[voResult.size()];
                for (int i = 0; i < voResult.size(); i++) {
                        Map<String, Object> voObject = voResult.get(i); 
                        vaValue[i] = ((BigDecimal) voObject.get("ZONE_ID")).toString();
                        vaMessage[i] = (String) voObject.get("ZONE_NAME");
                }
                JSONObject voObject = new JSONObject();
                JSONArray voList = new JSONArray();
                PrintWriter voOut = response.getWriter();
                if (vaMessage != null) {
                        for (int i = 0; i < vaMessage.length; i ++) {
                                voList.add(vaMessage[i]);
                        }
                }
                voObject.put("messages", voList);
                JSONArray voList2 = new JSONArray();
                if (vaValue != null) {
                        for (int i = 0; i < vaValue.length; i ++) {
                                voList2.add(vaValue[i]);
                        }
                }
                voObject.put("values", voList2);
                db.close();
                voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getRoute(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8"); 
                response.setContentType("text/JSON");
                String vaZoneCode = request.getParameter("zoneCode");
                String[] vaMessage = null, vaValue = null;
                Database db = new Database();
        
        RouteDAO voRouteDAO = new RouteDAO(db);
                List<Map<String, Object>> voResult = voRouteDAO.findRouteByZone(vnSiteId, vaZoneCode.equals("") ? 0 : Integer.parseInt(vaZoneCode));
                vaValue = new String[voResult.size()];
                vaMessage = new String[voResult.size()];
                for (int i = 0; i < voResult.size(); i++) {
                        Map<String, Object> voObject = voResult.get(i); 
                        vaValue[i] = ((BigDecimal) voObject.get("ROUTE_ID")).toString();
                        vaMessage[i] = (String) voObject.get("ROUTE_DESCRIPTION");
                }
                JSONObject voObject = new JSONObject();
                JSONArray voList = new JSONArray();
                PrintWriter voOut = response.getWriter();
                if (vaMessage != null) {
                        for (int i = 0; i < vaMessage.length; i ++) {
                                voList.add(vaMessage[i]);
                        }
                }
                voObject.put("messages", voList);
                JSONArray voList2 = new JSONArray();
                if (vaValue != null) {
                        for (int i = 0; i < vaValue.length; i ++) {
                                voList2.add(vaValue[i]);
                        }
                }
                voObject.put("values", voList2);
                db.close();
                voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getStatus(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8"); 
                response.setContentType("text/JSON");
                String[] vaMessage = null, vaValue = null;
                Database db = new Database();
        
                SMListOfValueDAO voSmLOV = new SMListOfValueDAO(db);
                List<Map<String, Object>> voResult = voSmLOV.findLOVByType("cust_stts");
                vaValue = new String[voResult.size()];
                vaMessage = new String[voResult.size()];
                for (int i = 0; i < voResult.size(); i++) {
                        Map<String, Object> voObject = voResult.get(i); 
                        vaValue[i] = (String) voObject.get("LOV_CODE");
                        vaMessage[i] = (String) voObject.get("LOV_NAME");
                }
                JSONObject voObject = new JSONObject();
                JSONArray voList = new JSONArray();
                PrintWriter voOut = response.getWriter();
                if (vaMessage != null) {
                        for (int i = 0; i < vaMessage.length; i ++) {
                                voList.add(vaMessage[i]);
                        }
                }
                voObject.put("messages", voList);
                JSONArray voList2 = new JSONArray();
                if (vaValue != null) {
                        for (int i = 0; i < vaValue.length; i ++) {
                                voList2.add(vaValue[i]);
                        }
                }
                voObject.put("values", voList2);
                db.close();
                voOut.print(voObject.toJSONString());
    }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                processRequest(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                processRequest(request, response);
        }

}